package com.onemile.controls;

import java.util.ArrayList;

import com.onemile.app.R;
import com.onemile.entity.Session;
import com.onemile.entity.model.EOrderForList;
import com.onemile.utils.UtiDateTime;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class AdpLV_OrderList extends BaseAdapter
{
   private ArrayList<EOrderForList> mList;
   private LayoutInflater mInflater;
   private Context mContext;
   private AdapterCallBack mCallBack;

   public interface AdapterCallBack
   {
	  public void Do(String OrderId);
   }

   public AdpLV_OrderList(Context context, AdapterCallBack callBack)
   {
	  mList = new ArrayList<EOrderForList>();
	  mContext = context;
	  mInflater = LayoutInflater.from(context);
	  mCallBack = callBack;
   }

   public void addItem(ArrayList<EOrderForList> _list)
   {
	  if (mList != null)
	  {
		 for (EOrderForList eOrder : _list)
		 {
			mList.add(eOrder);
		 }
	  }
   }

   @Override
   public int getCount()
   {
	  if (mList == null)
	  {
		 return 0;
	  }
	  return mList.size();
   }

   @Override
   public Object getItem(int position)
   {
	  return mList.get(position);
   }

   @Override
   public long getItemId(int position)
   {
	  return 0;
   }

   @Override
   public View getView(int position, View currentView, ViewGroup parent)
   {
	  try
	  {
		 ViewHolder holder;
		 if (currentView == null)
		 {
			currentView = mInflater.inflate(R.layout.orderlist_item, null);
			holder = new ViewHolder();
			holder.ShopName = (TextView) currentView.findViewById(R.id.txtShopName_orderlist_item);
			holder.ShopType= (TextView)currentView.findViewById(R.id.txtShopType_orderlist_item);				  
			holder.Date = (TextView) currentView.findViewById(R.id.txtDate_orderlist_item);
			holder.State = (TextView) currentView.findViewById(R.id.txtState_orderlist_item);
			holder.Commodities = (TextView) currentView.findViewById(R.id.txtCommodities_orderlist_item);
			holder.TotalPrice = (TextView) currentView.findViewById(R.id.txtTotalPrice_orderlist_item);
			holder.ImgOrderAgain = (ImageView) currentView.findViewById(R.id.imgOrderAgain_orderlist_item);

			currentView.setTag(holder);
		 }
		 else
		 {
			holder = (ViewHolder) currentView.getTag();
		 }

		 holder.ShopName.setText(mList.get(position).getShopsName());
		 holder.ShopType.setText(mList.get(position).getShopsType());
		 holder.Date.setText(UtiDateTime.DateToStr(mList.get(position).getRec_ModifyTime()));
		 switch (mList.get(position).getOrderType())
		 {
			case 1:
			   holder.State.setText("未确认");
			   break;
			case 2:
			   holder.State.setText(".确认");
			   break;
			case 4:
			   holder.State.setText("已送达");
			   break;
			case 8:
			   holder.State.setText("取消");
			   break;
		 }
		 holder.Commodities.setText(mList.get(position).getCommodities());
		 holder.TotalPrice.setText(String.valueOf(mList.get(position).getTotalPrice()));
		 

		 holder.ImgOrderAgain.setTag(position);
		 holder.ImgOrderAgain.setOnClickListener(new View.OnClickListener()
		 {
			@Override
			public void onClick(View v)
			{
			   if (Session.getInstance().IsLogin())
			   {
				  mCallBack.Do(mList.get((Integer) v.getTag()).getOrderId());
			   }
//			   else
//			   {
//				  Intent intent = new Intent();
//				  intent.setClass(mContext, UILogin.class);
//				  intent.putExtra(PubConst.Intent_Key.LOGIN_FROM_ACTIVITY, PubConst.ActivityCode.LOGIN);
//				  mContext.startActivity(intent);
//			   }
			}
		 });
		 return currentView;

	  }
	  catch (Exception e)
	  {
		 if (Session.getInstance().IsDEBUG())
		 {
			Log.e("ERROR", "AdpLV_OrderList_getView(position, currentView, parent) ", e);
		 }
	  }

	  return new View(mContext);
   }

   static class ViewHolder
   {
	  TextView ShopName;
	  TextView ShopType;
	  TextView Date;
	  TextView State;
	  TextView Commodities;
	  TextView TotalPrice;
	  ImageView ImgOrderAgain;

   }

}
